package com.example.asus.componentizationtest;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.webkit.WebView;
import android.widget.Button;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.launcher.ARouter;
import com.example.constants.Constants;
import com.example.event.OpenDialogEvent;
import com.example.home.mvp.view.activity.HMainActivity;
import com.example.interfaces.IService;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class MainActivity extends AppCompatActivity {

    @BindView(R.id.home_btn)
    Button homeBtn;
    @BindView(R.id.chat_btn)
    Button chatBtn;
    @BindView(R.id.find_btn)
    Button findBtn;
    @BindView(R.id.mine_btn)
    Button mineBtn;
    @BindView(R.id.webView_url)
    WebView webView;
    @BindView(R.id.home_mvp_btn)
    Button mvpBtn;


    @Autowired(name = Constants.JUMP_TARGRT_ACTIVIT_UTL)
    IService service;

    private Uri uri;


    @Subscribe(threadMode = ThreadMode.MAIN)
    public void openDialogEvent(final OpenDialogEvent openDialogEvent){
        AlertDialog.Builder builder  = new AlertDialog.Builder(this);
        builder .setTitle("请做出选择").setIcon(com.example.commonlib.R.mipmap.ic_launcher);
        builder .setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                openDialogEvent.callback.onInterrupt(null);
            }
        });

        builder .setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                openDialogEvent.callback.onContinue(openDialogEvent.postcard);
            }
        });
        AlertDialog dialog = builder.create();
        dialog.show();
        Button btnPos = dialog.getButton(DialogInterface.BUTTON_POSITIVE);
        Button btnNeg = dialog.getButton(DialogInterface.BUTTON_NEGATIVE);
        btnPos.setTextColor(Color.BLUE);
        btnNeg.setTextColor(Color.RED);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if(!EventBus.getDefault().isRegistered(this)){
            EventBus.getDefault().register(this);
        }
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        ARouter.getInstance().inject(this);
        webView.loadDataWithBaseURL("",Constants.URL3,"text/html","utf-8","");
    }


    @OnClick({R.id.home_btn,R.id.chat_btn,R.id.find_btn,R.id.mine_btn,R.id.home_url1_btn,R.id.home_url2_btn,R.id.home_mvp_btn})
    public void onClick(View view){
        switch (view.getId()){
            case R.id.home_btn:
//                ARouter.getInstance()
//                        .build("/home/main")
//                        .withString("home","我喜欢聊天！")
//                        .navigation();
                service.jumpActivity(this,Constants.HOME_ACTIVITY_URL,"home","我喜欢聊天！");
//                startActivity(new Intent(this, HomeMainActivity.class).putExtra("home","我的家在东北！"));
                break;
            case R.id.chat_btn:
//                ARouter.getInstance()
//                        .build("/chat/main")
//                        .withString("chat","我喜欢聊天！")
//                        .navigation();
                service.jumpActivity(this,Constants.CHAT_ACTIVITY_URL,"chat","我喜欢聊天！");
//                startActivity(new Intent(this, ChatMainActivity.class).putExtra("chat","我喜欢聊天！"));
                break;
            case R.id.find_btn:
//                ARouter.getInstance()
//                        .build("/find/main")
//                        .navigation();
                service.jumpActivity(this,Constants.FIND_ACTIVITY_URL,"","");
//                startActivity(new Intent(this, FindMainActivity.class));
                break;
            case R.id.mine_btn:
//                ARouter.getInstance()
//                        .build("/mine/main")
//                        .navigation();
                service.jumpActivity(this,Constants.MINE_ACTIVITY_URL,"","");
//                startActivity(new Intent(this, MineMainActivity.class));
                break;
            case R.id.home_url1_btn:
                uri = Uri.parse(Constants.URL1);
                service.jumpActivity2(this,uri);
                break;
            case R.id.home_url2_btn:
                uri = Uri.parse(Constants.URL2);
                service.jumpActivity2(this, uri);
                break;

            case R.id.home_mvp_btn:
                startActivity(new Intent(this, HMainActivity.class));
                break;
        }
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}
